package display.threads;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.Semaphore;

import structs.book.Book;
import Util.Util;
import display.ImportDialog;


public class MBSThread extends Thread {
	private File f;
	private String MATKEY, MATSEQ, MMBSNO, MMBSCK,
				   AUTHOR, TITLE, ED, TYPE, PUB,
				   SUBPUB, ISBN, ISBNCK, LIST,
				   LISTFLAG, REFFLAG, REF, RELFL,
				   ACT, NTXFL, COMMENT, GUIDE,
				   EDSTATUS, TXTRD, LMT;
	private static ImportDialog id = new ImportDialog();
	private Semaphore finish;
	
	public MBSThread(File fin, Semaphore sem) {
		f = fin;
		finish = sem;
		id.addFile(fin.getName());
	}
	
	@Override
	public void run() {
		id.setVisible(true);
		
		id.setStatus(f.getName(),"Parsing");
		ArrayList<Book> books = parse();
		id.setCount(f.getName(),books.size());
		
		id.setStatus(f.getName(),"Generating");
		id.insert(books);
		
		//System.out.println("Finished importing file "+f.getName());
		
		id.setStatus(f.getName(),"Finished");
		finish.release();
	}

	/**
	 * Parses the input file and returns an ArrayList of the books read in.
	 * @param books
	 */
	private ArrayList<Book> parse() {
		Util.printStatusString("parsing");
		ArrayList<Book> books = new ArrayList<Book>();
		Scanner sc;
		try {
			//int count = 0;
			sc = new Scanner(new FileInputStream(f)).useDelimiter("\t|\n");
		
			while (sc.hasNext()) {
				
				readLine(sc);
				books.add(new Book(clean(AUTHOR),
									clean(TITLE),
							  Long.valueOf(ISBN),
							  		   clean(ED),
							  		  		""));	//TODO:inv_num is unknown at this point.

			}
			
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		Util.printStatusString("parsing finished");
		return books;
	}
	
	private void readLine(Scanner sc) {
		MATKEY = sc.next();
		MATSEQ = sc.next();
		MMBSNO = sc.next();
		MMBSCK = sc.next();
		AUTHOR = sc.next();
		TITLE = sc.next();
		ED = sc.next();
		TYPE = sc.next();
		PUB = sc.next();
		SUBPUB = sc.next();
		ISBN = sc.next();
		ISBNCK = sc.next();
		LIST = sc.next();
		LISTFLAG = sc.next();
		REFFLAG = sc.next();
		REF = sc.next();
		RELFL = sc.next();
		ACT = sc.next();
		NTXFL = sc.next();
		COMMENT = sc.next();
		GUIDE = sc.next();
		EDSTATUS = sc.next();
		TXTRD = sc.next();
		LMT = sc.next();
	}
	
	/**
	 * Removes single quotes (') from the given string.
	 * @param s
	 * @return
	 */
	private String clean(String s) {
		return s.replace('\'', ' ');
	}
	
	private void printLine() {
		System.out.print("AT: "+MATKEY+", ");
		System.out.print("ATS: "+MATSEQ+", ");
		System.out.print("MBSNO: "+MMBSNO+", ");
		System.out.print("MBSCK: "+MMBSCK+", ");
		System.out.print("AUTHOR: "+AUTHOR+", ");
		System.out.print("TITLE: "+TITLE+", ");
		System.out.print("ED: "+ED+", ");
		System.out.print("TYPE: "+TYPE+", ");
		System.out.print("PUB: "+PUB+", ");
		System.out.print("SPUB: "+SUBPUB+", ");
		System.out.print("ISBN: "+ISBN+", ");
		System.out.print("ISBNCK: "+ISBNCK+", ");
		System.out.print("LIST: "+LIST+", ");
		System.out.print("LISTFLG: "+LISTFLAG+", ");
		System.out.print("REFFLAG: "+REFFLAG+", ");
		System.out.print("REF: "+REF+", ");
		System.out.print("RELFL: "+RELFL+", ");
		System.out.print("ACT: "+ACT+", ");
		System.out.print("NTXFL: "+NTXFL+", ");
		System.out.print("COMMENT: "+COMMENT+", ");
		System.out.print("GUIDE: "+GUIDE+", ");
		System.out.print("EDSTATUS: "+EDSTATUS+", ");
		System.out.print("TXTRD: "+TXTRD+", ");
		System.out.print("LMT: "+LMT+"\n");
	}
}
